// http 模块  实现web服务
const http = require('http');
const url = require('url');
const fs = require('fs');

// 创建服务 
const server = http.createServer((req,res)=>{
    // console.log(url.parse(req.url,true));
    const {query,pathname} = url.parse(req.url,true);
    // console.log(query);{name:'zhangsan',age:18}
    // console.log(pathname);  /home
    // 如果访问的是 /home   home.html
    if(pathname === '/home'){
        fs.readFile('client\\views\\home.html','utf-8',(err,data)=>{
            if(err){
                return console.log(err)
            }
            res.end(data)
        })
    }

    // 如果访问的是 /detail  detail.html
    if(pathname === '/detail'){
        fs.readFile('client\\views\\detail.html','utf-8',(err,data)=>{
            if(err){
                return console.log(err)
            }
            res.end(data)
        })
    }


    if(pathname === '/list'){
        fs.readFile('client\\views\\list.html','utf-8',(err,data)=>{
            if(err){
                return console.log(err)
            }
            res.end(data)
        })
    }
})



//启动服务 监听用户访问

server.listen(8080,()=>{
    console.log('服务启动成功,正在监听8080端口');
})